import pygame
import random
import time

def draw(x,y,z):
    #a=random.randint(0,255)
    #b=random.randint(0,255)
    #c=random.randint(0,255)
    a=255
    b=0
    c=0
    for i in range(0,z):
        if i==0:
            pygame.draw.circle(screen,[a,b,c],[x[i],y[i]],27,0)
        if i!=0:
            pygame.draw.circle(screen,[a,b,c],[x[i],y[i]],23,0)
pygame.init()

def drawo(x,y,z):
    for i in range(0,z):
        pygame.draw.circle(screen,[255,255,255],[x[i],y[i]],27,0)
pygame.init()


"""
def eatx(x,z):
    e_x=random.randint(0,9)
    e_x=e_x*60
    e_x+=30
    r=True
    i=1
    z+=1
    while r:
        r=False
        i=1
        e_x=random.randint(0,9)
        e_x=e_x*60
        e_x+=30
        
        while i < z:
            if x[i]==e_x:
                r=True
            i+=1
        
        for a in range[i,z]:
            if x[a]==e_x:
                r=True

    return e_x

def eaty(y,z):
    e_y=random.randint(0,9)
    e_y=e_y*60
    e_y+=30
    r=True
    while r:
        r=False
        i=1
        e_y=random.randint(0,9)
        e_y=e_y*60
        e_y+=30
        
        while i < z:
            if y[i]==e_y:
                r=True
            i+=1
       
        for a in range[i,z+1]:
            if y[a]==e_y:
                r=True
    return e_y
    
    for i in range(0,z):
        if y[i]==e_y:
            e_y=random.randint(0,9)
            e_y=e_y*60
            e_y+=30
    for i in range(0,z):
        if y[i]==e_y:
            r=True
        else:
            r=False
        
    return e_y

def eatx(x,y,z):
    exy=[]
    for ii in range(0,10):
        for iii in range(0,10):
            e_x=ii
            e_y=iii
            e_xy=str(e_x)+str(e_y)
            exy.append(e_xy)
    for i in range(0,z):
        x_1=int((x[i]-30)/60)
        y_1=int((y[i]-30)/60)
        x_1=str(x_1)
        y_1=str(y_1)
        xy=x_1+y_1
        if xy in exy:
            exy.remove(xy)
    r=True
    while r:
        xy=str(random.randint(0,99))
        if xy in exy:
            r=False
        else:
            pass
    return xy
fp="python3/study/base.txt"
f=open(fp,"r+")
c_x=int(f.readline(100))
c_y=int(f.readline(100))
f=open(fp,"w+")
s=str(c_x)
s2=str(c_y)
f.writelines([s+"\n",s2+"\n"])
f.close()
"""
def eatx(x,y,z):
    c=1
    exy=[]
    for ii in range(0,10):
        for iii in range(0,10):
            e_x=ii
            e_y=iii
            e_xy=str(e_x)+str(e_y)
            exy.append(e_xy)
    for i in range(0,z+1):
        x_1=int((x[i]-30)/60)
        y_1=int((y[i]-30)/60)
        x_1=str(x_1)
        y_1=str(y_1)
        xy=x_1+y_1
        if xy in exy:
            exy.remove(xy)
    r=True
    while r:
        if c<40:
            xy=str(random.randint(0,99))
            if xy in exy:
                r=False
            else:
                c+=1
        if c>40:
            fp="python3/study/02.txt"
            f=open(fp,"r+")
            xy=int(f.readline(10))
            if xy in exy:
                r=False
            else:
                xy+=1
                if xy>99:
                    xy=0
                f=open(fp,"w+")
                s=str(xy)
                f.writelines(s)
                f.close()
    return xy

def shu(a,s,d,f):
    a_100=0
    a_200=0
    a_300=0
    a_400=0
    if a:
        a_100=1
    if s:
        a_200=1
    if d:
        a_300=1
    if f:
        a_400=1
    if a_100+a_200+a_300+a_400<1:
        return True
    else:
        return False

pygame.display.set_caption("贪吃蛇 good luck")
screen=pygame.display.set_mode([600,600])
screen.fill([255,255,255])

x=[30]
y=[30]
z=1
z_1=80
"""
"""
zuo=True
you=True
shang=True
xia=True
"""
"""
for iii in range(0,z_1):
    x.append(0)
    y.append(0)
fenshu=0

exy=eatx(x,y,z)
#zgy=["翟光亚","45","100"]
#zgy[1]=46
#class ren {
#  def __init__(name,year,weith):
#      self.name=name
#      self.year=year
#      self.weight=weight
#}
# zgy=ren("翟光亚",45,"100")
# zgy.year=46
#class node {
#  def __init__(x,y):
#      self.x=x
#      self.y=y
#  def move(self,xshift,yshift):
#      self.x+=xshift
#      self.y+=yshift
#}
# snake=[node(1,1),node(1,2),node(1,3),node(1,4),node(1,5)]
# for i in snake:
#     snake[i].move(1,0)

def draweat(x):
    z=0
    for i in x:
        if z==0:
            ex=i
            z+=1
        if z!=0:
            ey=i
    x=int(ex)
    y=int(ey)
    x=x*60+30
    y=y*60+30
    #pygame.draw.circle(screen,[0,0,0],[x+25,y-25],25,0)
    #pygame.draw.circle(screen,[255,255,255],[x+25,y-25],24,0)
    #pygame.draw.rect(screen,[255,255,255],[x+45,y-50,5,50],50)
    pygame.draw.circle(screen,[255,0,255],[x,y],23,0)
    pygame.display.flip()
draweat(exy)
pygame.display.flip()

for chang in range(0,10):
    chang = chang*60
    for kuan in range(0,10):
        kuan = kuan*60
        pygame.draw.rect(screen,[0,255,0],[chang,kuan,60,60],1)
        pygame.display.flip()


draw(x,y,z)
pygame.display.flip()
running=True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running=False
        elif event.type == pygame.KEYDOWN:
            #pygame.draw.circle(screen, [255,255,255], [x,y],25)
            # print(event.key)
            #
            """"""
            draweat(exy)
            draw(x,y,z)
            pygame.display.flip()
            if shu(shang,xia,zuo,you):
                print("你输啦")
                print(f"分数:{fenshu}")
                time.sleep(2)
                running=False
                break
            if y[0]-60<=0:
                shang=False
            if y[0]-60>0:
                shang=True

            if y[0]+60>=600:
                xia=False
            if y[0]+60<600:
                xia=True

            if x[0]-60<=0:
                zuo=False
            if x[0]-60>0:
                zuo=True

            if x[0]+60>=600:
                you=False
            if x[0]+60<600:
                you=True
            
            for i in range(1,z):
                if shang:
                    if y[0]-60==y[i]:
                        if x[0]==x[i]:
                            shang=False
                            continue
                    if y[0]-60!=y[i]:
                        shang=True

            for i in range(1,z):
                if xia:
                    if y[0]+60==y[i]:
                        if x[0]==x[i]:
                            xia=False
                            continue
                    if y[0]+60!=y[i]:
                        xia=True

            for i in range(1,z):
                if zuo:
                    if x[0]-60==x[i]:
                        if y[0]==y[i]:
                            zuo=False
                            continue
                    if x[0]-60!=x[i]:
                        zuo=True

            for i in range(1,z):
                if you:
                    if x[0]+60==x[i]:
                        if y[0]==y[i]:
                            you=False
                            continue
                    if x[0]+60!=x[i]:
                        you=True
            if shu(shang,xia,zuo,you):
                print("你输啦")
                print(f"分数:{fenshu}")
                time.sleep(2)
                running=False
                break
                
            
            """"""
            if event.key == pygame.K_UP:
                #print("向上")
                #if y[0]-60>0:
                if shang:
                    drawo(x,y,z)
                    pygame.display.flip()
                    x_1=x[0]
                    y_1=y[0]-60
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
            elif event.key == pygame.K_DOWN:
                #print("向下")
                #if y[0]+60<600:
                if xia:
                    drawo(x,y,z)
                    pygame.display.flip()
                    x_1=x[0]
                    y_1=y[0]+60
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
            elif event.key == pygame.K_LEFT:
                #print("向左")
                #if x[0]-60>0:
                if zuo:
                    drawo(x,y,z)
                    pygame.display.flip()
                    #for i in range(0,z):                         
                     #   x[i]-=1*60
                    x_1=x[0]-60
                    y_1=y[0]
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
            elif event.key == pygame.K_RIGHT:
                #print("向右")
                #if x[0]+60<600:
                if you:
                    drawo(x,y,z)
                    pygame.display.flip()
                    x_1=x[0]+60
                    y_1=y[0]
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
            sxy=str(int((x[0]-30)/60))+str(int((y[0]-30)/60))
            draweat(exy)
            draw(x,y,z)
            pygame.display.flip()
            #print(sxy,exy)
            if sxy==exy:
                fenshu+=1
                #print(fenshu)
                exy=eatx(x,y,z)
                z+=1
                if z>=z_1:
                    print("你赢了")
                    print(f"分数:{fenshu}")
                    running=False
                    time.sleep(1)
            if shu(shang,xia,zuo,you):
                print("你输啦")
                print(f"分数:{fenshu}")
                time.sleep(2)
                running=False

                break
            draweat(exy)
            draw(x,y,z)
            pygame.display.flip()


pygame.quit
pygame.quit()




"""



import pygame
import random
import time

def draw(x,y,z):
    for i in range(0,z):
        pygame.draw.circle(screen,[255,0,0],[x[i],y[i]],25,0)
pygame.init()

def drawo(x,y,z):
    for i in range(0,z):
        pygame.draw.circle(screen,[255,255,255],[x[i],y[i]],25,0)
pygame.init()



def eatx(x):
    e_x=random.randint(0,9)
    e_x=e_x*60
    e_x+=30
    r=True
    while r:
        for i in range(0,z):
            if x[i]==e_x:
                e_x=random.randint(0,9)
                e_x=e_x*60
                e_x+=30
        for i in range(0,z):
            if x[i]==e_x:
                r=True
            else:
                r=False
    return e_x

def eaty(y):
    e_y=random.randint(0,9)
    e_y=e_y*60
    e_y+=30
    r=True
    while r:
        for i in range(0,z):
            if y[i]==e_y:
                e_y=random.randint(0,9)
                e_y=e_y*60
                e_y+=30
        for i in range(0,z):
            if y[i]==e_y:
                r=True
            else:
                r=False
    return e_y

pygame.display.set_caption("贪吃蛇 good luck")
screen=pygame.display.set_mode([600,600])
screen.fill([255,255,255])

x=[30]
y=[30]
z=1
z_1=100
"""
"""
zuo=True
you=True
shang=True
xia=True
zuo_1=False
you_1=True
shang_1=False
xia_1=False
"""
"""
for iii in range(0,z_1):
    x.append(0)
    y.append(0)
fenshu=0
ex=eatx(x)
ey=eaty(y)

def draweat(x,y):
    pygame.draw.circle(screen,[255,0,255],[x,y],25,0)
    pygame.display.flip()
draweat(ex,ey)
pygame.display.flip()

for chang in range(0,10):
    chang = chang*60
    for kuan in range(0,10):
        kuan = kuan*60
        pygame.draw.rect(screen,[0,255,0],[chang,kuan,60,60],1)
        pygame.display.flip()

clock = pygame.time.Clock()
draw(x,y,z)
pygame.display.flip()
running=True
while running:
    for event in pygame.event.get():
        clock.tick(3)
        if event.type == pygame.QUIT:
            running=False
        elif event.type == pygame.KEYDOWN:
            #pygame.draw.circle(screen, [255,255,255], [x,y],25)
            # print(event.key)
            #
            
            
            if y[0]-60<=0:
                shang=False
            if y[0]-60>0:
                shang=True

            if y[0]+60>=600:
                xia=False
            if y[0]+60<600:
                xia=True

            if x[0]-60<=0:
                zuo=False
            if x[0]-60>0:
                zuo=True

            if x[0]+60>=600:
                you=False
            if x[0]+60<600:
                you=True
            
            for i in range(1,z):
                if shang:
                    if y[0]-60==y[i]:
                        if x[0]==x[i]:
                            shang=False
                            continue
                    if y[0]-60!=y[i]:
                        shang=True

            for i in range(1,z):
                if xia:
                    if y[0]+60==y[i]:
                        if x[0]==x[i]:
                            xia=False
                            continue
                    if y[0]+60!=y[i]:
                        xia=True

            for i in range(1,z):
                if zuo:
                    if x[0]-60==x[i]:
                        if y[0]==y[i]:
                            zuo=False
                            continue
                    if x[0]-60!=x[i]:
                        zuo=True

            for i in range(1,z):
                if you:
                    if x[0]+60==x[i]:
                        if y[0]==y[i]:
                            you=False
                            continue
                    if x[0]+60!=x[i]:
                        you=True
            a_100=0
            a_200=0
            a_300=0
            a_400=0
            if shang:
                a_100=1
            if xia:
                a_200=1
            if zuo:
                a_300=1
            if you:
                a_400=1
            if a_100+a_200+a_300+a_400<1:
                print("你输啦")
                time.sleep(2)
            
            
            if event.key == pygame.K_UP or shang_1:
                print("向上")
                #if y[0]-60>0:
                if shang:
                    drawo(x,y,z)
                    pygame.display.flip()
                    x_1=x[0]
                    y_1=y[0]-60
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
                    shang_1=True
                    xia_1=False
                    zuo_1=False
                    you_1=False
            elif event.key == pygame.K_DOWN or xia_1:
                print("向下")
                #if y[0]+60<600:
                if xia:
                    drawo(x,y,z)
                    pygame.display.flip()
                    x_1=x[0]
                    y_1=y[0]+60
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
                    shang_1=False
                    xia_1=True
                    zuo_1=False
                    you_1=False
            elif event.key == pygame.K_LEFT or zuo_1:
                print("向左")
                #if x[0]-60>0:
                if zuo:
                    drawo(x,y,z)
                    pygame.display.flip()
                    #for i in range(0,z):                         
                     #   x[i]-=1*60
                    x_1=x[0]-60
                    y_1=y[0]
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
                    shang_1=False
                    xia_1=False
                    zuo_1=True
                    you_1=False
            elif event.key == pygame.K_RIGHT or you:
                print("向右")
                #if x[0]+60<600:
                if you:
                    drawo(x,y,z)
                    pygame.display.flip()
                    x_1=x[0]+60
                    y_1=y[0]
                    x.insert(0,x_1)
                    y.insert(0,y_1)
                    del x[z_1]
                    del y[z_1]
                    shang_1=False
                    xia_1=False
                    zuo_1=False
                    you_1=True
            if x[0]==ex:
                if y[0]==ey:
                    fenshu+=1
                    print(fenshu)
                    ex=eatx(x)
                    ey=eaty(y)
                    z+=1
                    if z>=z_1:
                        print(f"分数:{fenshu}")
                        running=False
            draweat(ex,ey)
            draw(x,y,z)
            pygame.display.flip()


pygame.quit
















def chujie(x,y):
    i_1 = 0
    if x[i_1]<=0 or x[i_1]>=630:
        return True
    if y[i_1]<=0 or y[i_1]>=630:
        return True
    return False

#pygame.draw.circle(screen,[255,0,0],[100,200],50,0)
#pygame.display.flip()      pygame.draw.rect(screen,[0,255,0],[i*block_w,j*block_w,block_w,block_w],2)

import pygame, sys, random
from pygame.locals import *
pygame.init()
mainClock = pygame.time.Clock()
WINDOWWIDTH = 400
WINDOWHEIGHT = 400
rectLength = 18
windowSurface = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT),0,32)
pygame.display.set_caption('Snake')
BLACK = (0,0,0)
GREEN = (0,255,0)
snakeRect = []
for i in range(7,10):
    snakeRect.append(pygame.Rect(i*(rectLength+2)+1,0+1,rectLength,rectLength))
    food = pygame.Rect(5*(rectLength+2),5*(rectLength+2),rectLength+2,rectLength+2)
moveLeft = True
moveRight = False
moveUp = False
moveDown = False
direction = 1
foodImage = pygame.image.load('cherry.png')
pygame.mixer.music.load('background.mid')
pygame.mixer.music.play(-1,0.0)
pickUpSound = pygame.mixer.Sound('pickup.wav')
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT and moveRight==False:
                moveLeft = True
                moveRight = False
                moveUp = False
                moveDown = False
            if event.key == K_RIGHT and moveLeft==False:
                moveLeft = False
                moveRight = True
                moveUp = False
                moveDown = False
            if event.key == K_UP and moveDown==False:
                moveLeft = False
                moveRight = False
                moveUp = True
                moveDown = False
            if event.key == K_DOWN and moveUp==False:
                moveLeft = False
                moveRight = False
                moveUp = False
                moveDown = True
            head = pygame.Rect(snakeRect[0].left,snakeRect[0].top,snakeRect[0].width,snakeRect[0].height)
            if moveLeft == True:
                head.right = head.left-2
            if moveRight == True:
                head.left = head.right+2
            if moveUp == True:
                head.bottom = head.top-2
            if moveDown == True:
                head.top = head.bottom+2
                snakeRect.insert(0,head)
            if head.right<0 or head.left>WINDOWWIDTH or head.bottom<0 or head.top>WINDOWHEIGHT:
                break
            if food.left == snakeRect[0].left-1 and food.top == snakeRect[0].top-1:
                food.left = random.randint(0,WINDOWWIDTH/20-1)*(rectLength+2)
                food.top = random.randint(0,WINDOWHEIGHT/20-1)*(rectLength+2)
                pickUpSound.play()
            else:   
                snakeRect.pop(len(snakeRect)-1)
                windowSurface.fill(BLACK)
            for i in range(len(snakeRect)):
                pygame.draw.rect(windowSurface,GREEN,snakeRect[i])
                windowSurface.blit(foodImage,food)
            if food.left == 0 and food.right == 0:
                i = 3
                pygame.display.update()
            mainClock.tick(10)
"""
""" 

# -*- coding: utf-8 -*-
import pygame
import random
import copy

'''
    首先设置蛇的一个运行方向  接下来判断键盘事件在决定蛇的运行方向
    蛇可以运行起来了  那么接下来就是 吃食物增加自己的长度 和 不吃食物在不同的位置显示
'''
# 初始小蛇方向
move_up = False
move_down = False
move_left = False
move_right = True

# 1.1 游戏初始化
pygame.init()
clock = pygame.time.Clock()  # 设置游戏时钟
pygame.display.set_caption("贪吃蛇")  # 初始化标题
screen = pygame.display.set_mode((500, 500))  # 初始化窗口 窗体的大小为 500  500

# 1.2 初始化蛇的位置  蛇的长度  10 10 也就是蛇的 X Y 坐标
snake_list = [[10, 10]]

# 1.3 初始化食物的位置
x = random.randint(10, 10)
y = random.randint(10, 490)
food_point = [x, y]

# 1.4 开启游戏循环
running = True
while running:
    # 1.5 游戏时钟 刷新频率
    clock.tick(30)

    # 1.6 填充背景为白色
    screen.fill([255, 255, 255])

    # 1.9 绘制背景
    for x in range(0, 501, 10):
        pygame.draw.line(screen, (195, 197, 199), (x, 0), (x, 500), 1)
        pygame.draw.line(screen, (195, 197, 199), (0, x), (500, x), 1)

    # 1.7 绘制食物
    # pygame.draw.circle()
    # 用来画圆形，具体包括五个参数：
    # (1)画圆的表面，在本例中用screen创建了一个窗口，所以是画在screen表面上。
    # (2)用什么颜色来画，如用红色[255,0,0]。
    # (3)在什么位置画，[top,left]。
    # (4)直径。
    # (5)线宽，其中0表示完成填充。
    food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 25, 0)


    # 1.8 绘制蛇
    snake_rect = []
    for pos in snake_list:
        # 1.8.1 绘制蛇的身子
        snake_rect.append(pygame.draw.circle(screen, [255, 0, 0], pos, 5, 0))

        # 3.1 碰撞检测 如果蛇吃掉食物
        if food_rect.collidepoint(pos):
            # 贪吃蛇吃掉食物
            snake_list.append(food_point)
            # 重置食物位置
            food_point = [random.randint(10, 100), random.randint(1, 100)]
            food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 50, 0)
            break

    # # 2.1 移动位置 获取蛇头位置
    # pos = snake_list[0]

    # 2.4 获取蛇的长度，移动蛇的身子
    pos = len(snake_list) - 1
    while pos > 0:
        snake_list[pos] = copy.deepcopy(snake_list[pos - 1])
        pos -= 1

    # 2.2 更改蛇头位置
    if move_up:
        snake_list[pos][1] -= 10
        if snake_list[pos][1] < 0:
            snake_list[pos][1] = 100

    if move_down:
        snake_list[pos][1] += 10
        if snake_list[pos][1] > 500:
            snake_list[pos][1] = 0

    if move_left:
        snake_list[pos][0] -= 10
        if snake_list[pos][0] < 0:
            snake_list[pos][0] = 100

    if move_right:
        snake_list[pos][0] += 10
        if snake_list[pos][0] > 100:
            snake_list[pos][0] = 0
        
    pygame.draw.circle(screen, [100, 100, 100], [50,50], 10, 0)
    pygame.draw.circle(screen, [100, 100, 100], [450,450], 10, 0)
    if snake_list[pos][1]==50:
        if snake_list[pos][0] == 50:
            snake_list[pos][1] = 450
            snake_list[pos][0] = 450

    if snake_list[pos][1]==450:
        if snake_list[pos][0] == 450:
            snake_list[pos][1] = 50
            snake_list[pos][0] = 50

    pygame.draw.circle(screen, [10, 10, 10], [450,50], 10, 0)
    pygame.draw.circle(screen, [10, 10, 10], [250,250], 10, 0)
    pygame.draw.circle(screen, [10, 10, 10], [50,450], 10, 0)
    if snake_list[pos][1]>440 and snake_list[pos][1]<460:
        if snake_list[pos][0]>40 and snake_list[pos][0]<60:
            snake_list[pos][1] = 250
            snake_list[pos][0] = 250
            
    if snake_list[pos][1]>240 and snake_list[pos][1]<260:
        if snake_list[pos][1]>240 and snake_list[pos][1]<260:
            snake_list[pos][1] = 50
            snake_list[pos][0] = 450
    if snake_list[pos][1]>40 and snake_list[pos][1]<60:
        if snake_list[pos][1]>440 and snake_list[pos][1]<460:
            snake_list[pos][1] = 450
            snake_list[pos][0] = 50 

    
    if snake_list[pos][1]>40 and snake_list[pos][1]<60:
        if snake_list[pos][1]>440 and snake_list[pos][1]<460:
            snake_list[pos][1] = 
            snake_list[pos][0] =  
    
    # 2.3 键盘控制移动职位
    for event in pygame.event.get():
        # print(event)
        # 判断按下的按键
        if event.type == pygame.KEYDOWN:
            # 上键
            if event.key == pygame.K_UP:
                move_up = True
                move_down = False
                move_left = False
                move_right = False
            # 下键
            if event.key == pygame.K_DOWN:
                move_up = False
                move_down = True
                move_left = False
                move_right = False
            # 左键
            if event.key == pygame.K_LEFT:
                move_up = False
                move_down = False
                move_left = True
                move_right = False
            # 右键
            if event.key == pygame.K_RIGHT:
                move_up = False
                move_down = False
                move_left = False
                move_right = True

    # 3.2 如果蛇吃掉了自己
    head_rect = snake_rect[0]
    count = len(snake_rect)
    while count > 1:
        #if head_rect.colliderect(snake_rect[count - 1]):
         #   running = False
        count -= 10000

    pygame.display.update()
"""